package com.example.demo.service;

import com.example.demo.mapper.ReportMapper;
import com.example.demo.mapper.TorrentMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Report;
import com.example.demo.model.Torrent;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private TorrentMapper torrentMapper;

    @Autowired
    private UserMapper userMapper;

    public void createReport(Long torrentId, Long reporterId, String reason, String description) {
        Torrent torrent = torrentMapper.findById(torrentId);
        if (torrent == null) {
            throw new RuntimeException("种子不存在");
        }

        Report report = new Report();
        report.setTorrentId(torrentId);
        report.setReporterId(reporterId);
        report.setReason(reason);
        report.setDescription(description);

        reportMapper.insertReport(report);
    }

    public List<Report> getPendingReports() {
        return reportMapper.getReportsByStatus(0);
    }

    @Transactional
    public void processReport(Long reportId, Long adminId, String processResult, Integer actionTaken) {
        // 验证举报是否存在且未处理
        Report report = reportMapper.getReportById(reportId);
        if (report == null || report.getStatus() == 1) {
            throw new RuntimeException("举报不存在或已处理");
        }

        // 更新举报状态
        report.setStatus(1); // 标记为已处理
        report.setAdminId(adminId);
        report.setProcessTime(new Date());
        report.setProcessResult(processResult);
        report.setActionTaken(actionTaken);

        // 根据 actionTaken 执行相应操作
        switch (actionTaken) {
            case 0: // 无效举报
                // 仅更新举报状态，无需额外操作
                break;

            case 1: // 删除种子
                Torrent torrent = torrentMapper.findById(report.getTorrentId());
                if (torrent == null) {
                    throw new RuntimeException("种子不存在");
                }
                torrentMapper.deleteById(report.getTorrentId());
                break;

            case 2: // 封禁用户并删除种子
                torrent = torrentMapper.findById(report.getTorrentId());
                if (torrent == null) {
                    throw new RuntimeException("种子不存在");
                }
                User user = userMapper.getUserById(torrent.getUploaderId());
                if (user == null) {
                    throw new RuntimeException("用户不存在");
                }
                // 封禁用户
                user.setBanned(true);
                user.setBanReason(processResult); // 使用处理结果作为封禁原因
                user.setBanTime(LocalDateTime.now());
                userMapper.updateUser(user);
                // 删除种子
                torrentMapper.deleteById(report.getTorrentId());
                break;

            default:
                throw new IllegalArgumentException("无效的 actionTaken 值: " + actionTaken);
        }

        // 更新举报记录
        reportMapper.updateReport(report);
    }
}